package com.iaiai.tm.activity;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

/**
 * 
 * <p>
 * Title: CpuGraphView.java
 * </p>
 * <p>
 * E-Mail: 176291935@qq.com
 * </p>
 * <p>
 * QQ: 176291935
 * </p>
 * <p>
 * Http: iaiai.iteye.com
 * </p>
 * <p>
 * Create time: 2012-1-30 9:35:59
 * </p>
 * 
 * @author 丸子
 * @version 0.0.1
 */
public class CpuGraphView extends SurfaceView implements SurfaceHolder.Callback {
	
	private static final int OFFSET = 2;	//刷新偏移量，像素
	private static final int BACKGROUND_COLOR = Color.BLACK;	//背景色
	private static final int LINE_COLOR = R.color.LINE_COLOR;	//网格颜色
	private static final int HEARTBEAT_COLOR = R.color.HEARTBEAT_COLOR;	//心跳线颜色
	
	private boolean isStop = false;	//是否停止

	private int width = 0;
	private int height = 0;
	private Paint paint;	//网格
	
	private Paint paintHeartbeat;	//心跳线

	private SurfaceHolder holder;
	
	private int offset = 0;	//偏移量
	
	private List<Map<String,Integer>> list = new ArrayList<Map<String,Integer>>();

	public CpuGraphView(Context context) {
		super(context);
		// 生成paint
		paint = new Paint();
		paint.setColor(context.getResources().getColor(LINE_COLOR));
		paint.setAntiAlias(true);// 去掉边缘锯齿
		paint.setStrokeWidth(1); // 设置画笔宽度
		paintHeartbeat = new Paint();
		paintHeartbeat.setColor(context.getResources().getColor(HEARTBEAT_COLOR));
		paintHeartbeat.setAntiAlias(true);
		paintHeartbeat.setStrokeWidth(1);

		holder = this.getHolder();// 获取holder
		holder.addCallback(this);
	}

	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh) {
		width = w;
		height = h;
		super.onSizeChanged(w, h, oldw, oldh);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		canvas = holder.lockCanvas(null);
		canvas.translate(-offset, 0);
		offset=offset+OFFSET;
		try {
			canvas.setDrawFilter(new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG)); // 给Canvas加上抗锯齿标志

			// 填充整个画布
			canvas.drawColor(BACKGROUND_COLOR);

			// 计算中轴位置
//			int axis = height / 2;

			// 画竖线
			int w = 0;
			while (w < width+offset) {
				w += 10;
				canvas.drawLine(w, 0, w, height, paint);
			}

			// 画横线
			int h = 0;
			while (h < height) {
				h += 10;
//				if (h <= axis && (h + 10) > axis) { // 如果是中轴
//					paint.setStrokeWidth(2); // 设置画笔宽度
//					canvas.drawLine(0, h, width+offset, h, paint);
//					paint.setStrokeWidth(1); // 设置画笔宽度
//				} else {
					canvas.drawLine(0, h, width+offset, h, paint);
//				}
			}
			
			drawHeartbeat(canvas);	//画心跳
			
			canvas.save();// 每画完一个图做备忘存储,以保证与其它图形互不影响
		} catch (Exception e) {
		} finally {
			holder.unlockCanvasAndPost(canvas);// 解锁画布，提交画好的图像
		}
	}
	
	private Integer getData(){
		Integer num = 0;
		try {
			num = (int) (100*LinuxSystemTool.getCpuInfo());
			Thread.sleep(1000);
		}catch (Exception e) {
			e.printStackTrace();
		}
		return num;
	}
	
	/**
	 * 画心跳
	 * @param canvas
	 */
	private void drawHeartbeat(Canvas canvas){
		if(list.size()==1){
			canvas.drawLine(list.get(0).get("x"), list.get(0).get("y"), list.get(0).get("x"), list.get(0).get("y"), paintHeartbeat);
		}else{
			for(int i=0;i<list.size()-1;i++){
				canvas.drawLine(list.get(i).get("x"), list.get(i).get("y"), list.get(i+1).get("x"), list.get(i+1).get("y"), paintHeartbeat);
			}
		}
		
		Map<String,Integer> map = new HashMap<String,Integer>();
		map.put("x", width+offset);
		map.put("y", height-(height*getData()/100));
		list.add(map);
		
		//判断，如果记录太多则删除多余的
		if(list.size()>(width/OFFSET+10)){
			list.remove(0);
		}
	}

	// 自定义线程类
	private class MyThread implements Runnable {
		public void run() {
			while (!isStop) {
				onDraw(null);
			}
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * android.view.SurfaceHolder.Callback#surfaceCreated(android.view.SurfaceHolder
	 * )
	 */
	public void surfaceCreated(SurfaceHolder holder) {
		// 启动自定义线程
		new Thread(new MyThread()).start();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * android.view.SurfaceHolder.Callback#surfaceChanged(android.view.SurfaceHolder
	 * , int, int, int)
	 */
	public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.view.SurfaceHolder.Callback#surfaceDestroyed(android.view.
	 * SurfaceHolder)
	 */
	public void surfaceDestroyed(SurfaceHolder holder) {
		isStop = true;
	}

}
